package com.zkgl.rent.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zkgl.common.annotation.Log;
import com.zkgl.common.core.controller.BaseController;
import com.zkgl.common.core.domain.AjaxResult;
import com.zkgl.common.core.domain.entity.SysUser;
import com.zkgl.common.enums.BusinessType;
import com.zkgl.rent.domain.Contract;
import com.zkgl.rent.domain.RentBill;
import com.zkgl.rent.service.IRentBillService;
import com.zkgl.common.utils.poi.ExcelUtil;
import com.zkgl.common.core.page.TableDataInfo;

/**
 * 租金账单Controller
 * 
 * @author macro
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/rent/bill")
public class RentBillController extends BaseController {
    @Autowired
    private IRentBillService rentBillService;

    /**
     * 查询租金账单列表
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(RentBill rentBill) {
        startPage();
        List<RentBill> list = rentBillService.selectRentBillList(rentBill);
        return getDataTable(list);
    }

    /**
     * 导出租金账单列表
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:export')")
    @Log(title = "租金账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentBill rentBill) {
        List<RentBill> list = rentBillService.selectRentBillList(rentBill);
        ExcelUtil<RentBill> util = new ExcelUtil<RentBill>(RentBill.class);
        util.exportExcel(response, list, "租金账单数据");
    }

    /**
     * 获取租金账单详细信息
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        RentBill rentBill = rentBillService.selectRentBillById(id);
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(rentBill.getLandlordId())) {
                return error("无权查看此租金账单信息，如有疑问请联系管理员");
            }
        }
        return success(rentBillService.selectRentBillById(id));
    }

    /**
     * 新增租金账单
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:add')")
    @Log(title = "租金账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentBill rentBill) {
        rentBill.setLandlordId(getUserId());
        ;
        return toAjax(rentBillService.insertRentBill(rentBill));
    }

    /**
     * 修改租金账单
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:edit')")
    @Log(title = "租金账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentBill rentBill) {
        RentBill rentBillDb = rentBillService.selectRentBillById(rentBill.getId());
        // 判断是否为本人房源
        if (!SysUser.isAdmin(getUserId())) {
            if (!getUserId().equals(rentBillDb.getLandlordId())) {
                return error("无权查看此租金账单信息，如有疑问请联系管理员");
            }
        }
        return toAjax(rentBillService.updateRentBill(rentBill));
    }

    /**
     * 删除租金账单
     */
    @PreAuthorize("@ss.hasPermi('rent:bill:remove')")
    @Log(title = "租金账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rentBillService.deleteRentBillByIds(ids));
    }
}
